package com.andy.dao.impl;

import com.andy.dao.MyDao;
import com.andy.dao.entity.TAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class MyDaoImpl implements MyDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 卡号查询账户对象
     *
     * @param cardNo
     * @return
     * @throws Exception
     */
    @Override
    public TAccount queryAccountByCardNo(String cardNo) throws Exception {

        String sql = " SELECT * FROM T_ACCOUNT WHERE CARD_NO=? ";

        return jdbcTemplate.queryForObject(sql, new RowMapper<TAccount>() {
            @Override
            public TAccount mapRow(ResultSet rs, int rowNum) throws SQLException {
                TAccount account = new TAccount();
                account.setId(rs.getInt("ID"));
                account.setCardNo(rs.getString("CARD_NO"));
                account.setBalanceAmount(rs.getDouble("BALANCE_AMOUNT"));
                return account;
            }
        },cardNo);
    }

    /**
     * 修改单个账户余额
     * @param account
     * @throws Exception
     */
    @Override
    public int updateCustBalanceAmount(TAccount account) throws Exception {
        String sql = " UPDATE T_ACCOUNT SET BALANCE_AMOUNT=? WHERE ID=? ";
        return jdbcTemplate.update(sql,account.getBalanceAmount(),account.getId());
    }
}
